using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PickingGuide.Entity;
using PickingGuide.Model;
using Leeder.Service;
using Leeder.Repository;

namespace PickingGuide.Service
{
    /// <summary>
    /// 
    /// </summary>
    public class QueueInfoService : BaseService<QueueInfoModel>
    {
        /// <summary>
        /// 关联实体类型
        /// </summary>
        protected override Type RefEntityType { get { return typeof(QueueInfoEntity); } }

        /// <summary>
        /// 获取队列
        /// </summary>
        /// <param name="queueLength">队列长度</param>
        /// <returns></returns>
        public List<QueueInfoModel> GetWorkQueueAsc(string where, int queueLength)
        {
            try
            {
                var sql = "SELECT  * FROM TR_QueueInfo " + where + " ORDER BY CreatedDate ASC  OFFSET  0 ROWS FETCH NEXT " + queueLength + " ROWS ONLY";
                return RF<QueueInfoEntity>.SqlQueryable(sql).Select(s => new QueueInfoModel
                {
                    ID = s.ID,
                    Sequence = s.Sequence,
                    BodyNum = s.BodyNum,
                    CreatedDate = s.CreatedDate,
                    VIN = s.VIN,
                    CarCode = s.CarCode,
                    PartsCode1 = s.PartsCode1,
                    PartsCode2 = s.PartsCode2,
                    CarModel = s.CarModel,
                    Status = s.Status,
                    SyncFlag = s.SyncFlag,
                }).ToList();
            }
            catch (Exception ex)
            {
                LogObj.Error(ex.ToString());
                return default;
            }
        }

        /// <summary>
        /// 获取队列
        /// </summary>
        /// <param name="queueLength">队列长度</param>
        /// <returns></returns>
        public List<QueueInfoModel> GetWorkQueueDesc(string where, int queueLength)
        {
            try
            {
                var sql = "SELECT  * FROM TR_QueueInfo " + where + " ORDER BY CreatedDate DESC  OFFSET  0 ROWS FETCH NEXT " + queueLength + " ROWS ONLY";
                return RF<QueueInfoEntity>.SqlQueryable(sql).Select(s => new QueueInfoModel
                {
                    ID = s.ID,
                    Sequence = s.Sequence,
                    BodyNum = s.BodyNum,
                    CreatedDate = s.CreatedDate,
                    VIN = s.VIN,
                    CarCode = s.CarCode,
                    PartsCode1 = s.PartsCode1,
                    PartsCode2 = s.PartsCode2,
                    CarModel = s.CarModel,
                    Status = s.Status,
                    SyncFlag = s.SyncFlag,
                }).ToList();
            }
            catch (Exception ex)
            {
                LogObj.Error(ex.ToString());
                return default;
            }
        }

        public List<QueueInfoModel> GetQueueLater(QueueInfoModel model, int queueLength)
        {
            try
            {
                var sql = "SELECT  * FROM TR_QueueInfo where CreatedDate >= '" + model.CreatedDate + "' ORDER BY CreatedDate ASC  OFFSET  0 ROWS FETCH NEXT " + queueLength + " ROWS ONLY";
                return RF<QueueInfoEntity>.SqlQueryable(sql).Select(s => new QueueInfoModel
                {
                    ID = s.ID,
                    Sequence = s.Sequence,
                    BodyNum = s.BodyNum,
                    CreatedDate = s.CreatedDate,
                    VIN = s.VIN,
                    CarCode = s.CarCode,
                    PartsCode1 = s.PartsCode1,
                    PartsCode2 = s.PartsCode2,
                    CarModel = s.CarModel,
                    Status = s.Status,
                    SyncFlag = s.SyncFlag,
                }).ToList();
            }
            catch (Exception ex)
            {
                LogObj.Error(ex.ToString());
                return default;
            }
        }

        public List<QueueInfoModel> GetQueueBefore(QueueInfoModel model, int queueLength)
        {
            try
            {
                var sql = "SELECT  * FROM TR_QueueInfo where CreatedDate < '" + model .CreatedDate+ "' ORDER BY CreatedDate desc  OFFSET  0 ROWS FETCH NEXT " + queueLength + " ROWS ONLY";
                return RF<QueueInfoEntity>.SqlQueryable(sql).Select(s => new QueueInfoModel
                {
                    ID = s.ID,
                    Sequence = s.Sequence,
                    BodyNum = s.BodyNum,
                    CreatedDate = s.CreatedDate,
                    VIN = s.VIN,
                    CarCode = s.CarCode,
                    PartsCode1 = s.PartsCode1,
                    PartsCode2 = s.PartsCode2,
                    CarModel = s.CarModel,
                    Status = s.Status,
                    SyncFlag = s.SyncFlag,
                }).ToList();
            }
            catch (Exception ex)
            {
                LogObj.Error(ex.ToString());
                return default;
            }
        }

        /// <summary>
        /// 根据条件获取数据总量
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <returns></returns>
        public int GetQueueCountByWhere(string where)
        {
            return RF<QueueInfoEntity>.SqlQueryable("SELECT  * FROM TR_QueueInfo " + where).Count();
        }

        /// <summary>
        /// 根据条件获取指定分页指定数量的记录
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">单页数据数量</param>
        /// <returns></returns>
        public List<QueueInfoModel> GetQueueByPage(string where, int pageIndex, int pageSize)
        {
            if (pageIndex < 1)
                return null;
            pageIndex = (pageIndex - 1) * pageSize;
            return RF<QueueInfoEntity>.SqlQueryable("SELECT  * FROM TR_QueueInfo " + where + " ORDER BY CreatedDate DESC OFFSET " + pageIndex + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY ").Select(s => new QueueInfoModel
            {
                ID = s.ID,
                Sequence = s.Sequence,
                BodyNum = s.BodyNum,
                CreatedDate = s.CreatedDate,
                VIN = s.VIN,
                CarCode = s.CarCode,
                CarModel = s.CarModel,
                Status = s.Status,
                UpdateDate = s.UpdateDate,
                SyncFlag = s.SyncFlag,
            }).ToList();
        }
    }
}